﻿using Coldairarrow.Business.PB;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using Coldairarrow.Util.ClassLibrary;
using Coldairarrow.Util.Helper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PB
{
    [Route("/PB/[controller]/[action]")]
    public class PB_CirculationBillController : BaseApiController
    {
        #region DI

        private readonly RedisOptions _redisOptions; //1.声明实体
        private readonly MongoDBOptions _mongodbOptions;

        public PB_CirculationBillController(IPB_CirculationBillBusiness pB_CirculationBillBus, IOptions<MongoDBOptions> mongodbOptions, IOptions<RedisOptions> redisOptions)
        {
            _pB_CirculationBillBus = pB_CirculationBillBus;
            _mongodbOptions = mongodbOptions.Value;
            _redisOptions = redisOptions.Value;//3.赋值
        }

        IPB_CirculationBillBusiness _pB_CirculationBillBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<PB_CirculationBill>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _pB_CirculationBillBus.GetDataListAsync(input);
        }
        /// <summary>
        /// 生产维度查询报表
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_CirculationBillDataReportDTO>> GetCirculationBillDataReport(PageInput<PB_CirculationBillDataReportConditionDTO> input)
        {
            return await _pB_CirculationBillBus.GetCirculationBillDataReport(input);
        }
        /// <summary>
        /// 计划维度查询报表
        /// </summary>
        /// <param name="input">查询条件</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_CirculationBillDataReportDTO>> GetOrderDataReport(PageInput<PB_CirculationBillDataReportConditionDTO> input)
        {
            return await _pB_CirculationBillBus.GetOrderDataReport(input);
        }
        /// <summary>
        /// 获取钉钉用户工资明细
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<EmployeeSalaryQuery>> GetDingUserWages(EmployeeSalaryQuerySeach Seach) {
            List<EmployeeSalaryQuery> list = await _pB_CirculationBillBus.GetDingUserWages(Seach);
            return list;
        }
        [HttpPost]
        public async Task<PB_CirculationBill> GetTheData(IdInputDTO input)
        {
            return await _pB_CirculationBillBus.GetTheDataAsync(input.id);
        }


        /// <summary>
        /// 获取Pc端工序报工列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public  async Task<PageResult<PB_CirculationBillListDto>> GetDataCirculationBillList(PageInput<PB_CirculationBillListConditionDTO> input) { 
            return await _pB_CirculationBillBus.GetDataCirculationBillList(input);
        }
        /// <summary>
        /// 半成品入库流水
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_CirculationBillDetailDTO>> GetDataDTOList(PageInput<PB_CirculationBillConditionDTO> input)
        {
            return await _pB_CirculationBillBus.GetDataDTOListAsync(input);
        }
        /// <summary>
        /// 半成品余量
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_CirculationBillSurplusDTO>> GetDataSurplusDTOList(PageInput<PB_CirculationBillSurplusConditionDTO> input)
        {
            return await _pB_CirculationBillBus.GetDataSurplusDTOListAsync(input);
        }
        /// <summary>
        /// 获取喷涂作业中列表
        /// </summary>
        /// <param name="DeviceId">设备ID</param>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<List<CirculationBillList>> GetWorkingList(string DeviceId)
        {
            return await _pB_CirculationBillBus.GetWorkingListAsync(DeviceId);
        }

      /// <summary>
      /// 生产计划执行A1,A4车间
      /// </summary>
      /// <param name="data"></param>
      /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<List<ProductionPlanExecutionDto>> GetProductionPlaList(ParameterData data)
        {

            var list = await _pB_CirculationBillBus.GetProductionPlaList(data);
            return list;
        }
        /// <summary>
        /// 大屏显示指定车间当日是否有报工
        /// </summary>
        /// <param name="WorkShopName">车间名称,比如"A1车间","A4车间"</param>
        /// <param name="Days">前第几天的当日</param>
        /// <returns>"暂无数据"，""</returns>
        [HttpPost]
        [NoCheckJWT]
        public ScreenDisplayValue3DTO GetHaveData(string WorkShopName="", int Days=0)
        {
            return _pB_CirculationBillBus.GetHaveData(WorkShopName,Days);
        }
        /// <summary>
        /// 大屏显示当前三日各工序派工合格率
        /// </summary>
        /// <param name="WorkShopName">车间名称,比如"A1车间","A4车间"</param>
        /// <param name="Days">前第几天的当日</param>
        /// <returns>当前三日各工序派工合格率</returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<List<PB_DispatchDetailPassRateDTO>> GetTodayPassRateByName(string WorkShopName="", int Days=0)
        {
            return await _pB_CirculationBillBus.GetTodayPassRateByNameAsync(WorkShopName, Days);
        }
        /// <summary>
        /// 当前三日工单看板
        /// </summary>
        /// <param name="WorkShopName">车间名称</param>
        /// <param name="Days">前第几天的当日</param>
        /// <returns>当前三日各派工完成率</returns>
        [HttpPost]
        [NoCheckJWT]
        public async Task<List<PB_CirculationBillDispatchPadDTO>> GetTodayPassRate(string WorkShopName="", int Days=0)
        {           
            return await _pB_CirculationBillBus.GetTodayPassRateAsync(WorkShopName,Days);
        }
        /// <summary>
        /// 获取钉钉用户信息 计件工资调用
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [NoCheckJWT]
        public string GetDingdingUserPriceId(IdInputDTO input)
        {
            string token = DingHelper.getAccess_Token_Price();
            return DingHelper.getUserInfo(input.id, token);
        }
        /// <summary>
        /// 按照年月份获取钉钉用户报工数据
        /// </summary>
        /// <param name="Seach"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<EmployeeSalaryQueryMonth>> GetDingUserMonthWages(EmployeeSalaryQueryMonthSeach Seach) {
            List<EmployeeSalaryQueryMonth> list = await _pB_CirculationBillBus.GetDingUserMonthWages(Seach);
            return list;
        }
        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(PB_CirculationBill data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _pB_CirculationBillBus.AddDataAsync(data);
            }
            else
            {
                await _pB_CirculationBillBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _pB_CirculationBillBus.DeleteDataAsync(ids);
        }

        #endregion

        #region  半成品领料

        /// <summary>
        /// 判断派工单是否是自己的派工单
        /// </summary>
        /// <param name="DispatchDetailQrCode">派工单明细二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_DispatchDetail> GetDispatchDetail(string DispatchDetailQrCode)
        {
            return await _pB_CirculationBillBus.GetDispatchDetailAsync(DispatchDetailQrCode);
        }

        /// <summary>
        /// 获取流转卡对应的半成品信息
        /// </summary>
        /// <param name="CirBillQrCode">流转卡二维码</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ReceiveCirBillMaterialDTO> GetReceiveCirBillMaterialData(string CirBillQrCode)
        {
            return await _pB_CirculationBillBus.GetReceiveCirBillMaterialDataAsync(CirBillQrCode);
        }

        /// <summary>
        /// 半成品领取保存
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SaveReceiveMaterial(ReceiveMaterialDTO data)
        {
            await _pB_CirculationBillBus.SaveReceiveMaterialAsync(data);
        }

        /// <summary>
        /// 托盘领取详情
        /// </summary>
        /// <param name="trayId">托盘Id</param>
        /// <param name="CirBillId">流转卡Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<CirculationBillItemDTO>> GetCirculationBillItem(string trayId,string CirBillId)
        {
            return await _pB_CirculationBillBus.GetCirculationBillItemAsync(trayId, CirBillId);
        }

        #endregion


        /// <summary>
        /// 生成黑件包码物料码
        /// </summary>
        /// <param name="blackScanningDto"></param>
        /// <param name="_mongodbOptions"></param>
        /// <param name="_redisOptions"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<string>  SubmitBlackScanning(BlackScanningDto blackScanningDto) {
          return  await _pB_CirculationBillBus.SubmitBlackScanning(blackScanningDto, _mongodbOptions, _redisOptions);
        }
    }
}